﻿package fab.graphics 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	/**
	 * ...
	 * @author fab
	 */
	public class Background extends Sprite
	{
		private var repeat:Boolean=true;
		private var bitmapData:BitmapData;
		private var initialWidth:int;
		private var initialHeight:int;
		
		public function Background(_pix:BitmapData,_repeat:Boolean=true) 
		{
			repeat = _repeat;
			
			bitmapData = _pix
			//new BitmapData( _pix.width, _pix.height, false, 0x000000 );
			//bitmapData.draw( _pix );
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			initialWidth = stage.stageWidth;
			initialHeight = stage.stageHeight;

            //stage.addEventListener(Event.ACTIVATE, activateHandler);
            stage.addEventListener(Event.RESIZE, resizeHandler);
			draw_background();
			
		}
        private function activateHandler(event:Event):void
		{
        }
        private function resizeHandler(event:Event):void
		{
			draw_background();
        }
		private function draw_background():void
		{
			graphics.clear();        
			graphics.beginBitmapFill( bitmapData, null, repeat, false );  
			graphics.drawRect( -(stage.stageWidth - initialWidth ) / 2, -(stage.stageHeight - initialHeight ) / 2, stage.stageWidth, stage.stageHeight );
			graphics.endFill();
        }		
	}
}